package catchData;

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.log4j.Logger;

import util.CatchDataUtil;

/**
 * @author suwy
 * @date 2014-2-11
 * @todo TODO 抓取国家统计局数据
 */
public class CatchNationalData1 {

	private static final Logger LOG = Logger.getLogger(CatchNationalData1.class);
	private static String firstURL = "http://data.stats.gov.cn/quotas/getchildren?dbcode=hgnd&dimension=zb";
	private static String level1 = "&level=1&code=";
	
	private void excute() {
		HttpClient client = CatchDataUtil.initHttpClient();
		GetMethod firstGet = null;
		String[] codes = {"A0B","A0H","A0I","A0O","A0S"};//"A01","A02","A03","A04","A09","A0A"
		StringBuilder stringBuilder = new StringBuilder();
		for(int i = 0; i < codes.length; i++) {
			firstGet = new GetMethod(firstURL + level1 + codes[i]);
			CatchDataUtil.setHeaders(firstGet, firstURL + level1 + codes[i],
					CatchDataUtil.getWebSite(firstURL + level1 + codes[i]));
//			LOG.info(firstURL + level1 + codes[i]);
			try {
				client.executeMethod(firstGet);
				stringBuilder.append(firstGet.getResponseBodyAsString()
						.substring(1, firstGet.getResponseBodyAsString().length()-1) + ",");
			} catch (HttpException e) {
				LOG.info(e);
			} catch (IOException e) {
				LOG.info(e);
			} finally {
				firstGet.releaseConnection();
			}
		}
		LOG.info(stringBuilder);
	}
	
	public static void main(String[] args) {
		CatchNationalData1 instance = new CatchNationalData1();
		instance.excute();
	}
}